//
//  EeRotationView.swift
//  Aiwei
//
//  Created by zhu xietong on 2018/1/17.
//  Copyright © 2018年 zhu xietong. All rights reserved.
//

import Foundation
import Eelay

public enum EeRotationStatus {
    case end
    case rotationing
    case pause
}


open class EeRotationView:JoView{
    public let contentView = UIView()
    public var fps:Int = 30
    public var fps_degree:CGFloat = (1.0 / 360.0)

    

    public var timer:Timer?
    
    open func updateDegree(degree:CGFloat,status:EeRotationStatus) {
        
    }
    
    open var corner:CGFloat = 0{
        didSet{
            contentView.transform = CGAffineTransform(rotationAngle: corner)
            updateDegree(degree: corner,status: status)
        }
    }
    open var status:EeRotationStatus = .end{
        didSet{
            switch status {
            case .end:
                self.corner = 0
            case .pause:
                break
            case .rotationing:
                break
            }
            updateDegree(degree: corner,status: status)

        }
    }    
    
    open func startTimer() {
        self.timer = Timer(timeInterval: TimeInterval(1.0/CGFloat(fps)), target: self, selector: #selector(doRotation), userInfo: nil, repeats: true)
        RunLoop.current.add(self.timer!, forMode: RunLoop.Mode.common)
    }
    

    @objc open func doRotation(){
        switch self.status {
        case .rotationing:
            let value = fps_degree + self.corner
            self.corner = value
        default:
            break
        }
    }
    
    
    open func stopTimer() {
        self.timer?.invalidate()
        self.timer = nil
    }
    
    open override func addLayoutRules() {
        eelay = [
            [contentView,[ee.T.L.B.R]]
        ]
        startTimer()

    }
    
    open override func didMoveToWindow() {
        super.didMoveToWindow()
    }
    deinit {
        stopTimer()
    }
    
    
    
    
}
